package com.ruoyi.product.controller;

import java.util.List;

import com.ruoyi.product.domain.ProductSpecTemplateDetail;
import com.ruoyi.product.domain.request.TemplateCreateDto;
import com.ruoyi.product.service.IProductSpecTemplateService;
import com.ruoyi.product.service.TemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.product.domain.ProductSpecTemplate;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 规格属性模板Controller
 *
 * @author liuhuizhong
 * @date 2025-08-07
 */
@RestController
@RequestMapping("/product/productSpecTemplate")
@Tag(name = "规格属性模板")
public class ProductSpecTemplateController extends BaseController
{
    @Autowired
    private IProductSpecTemplateService productSpecTemplateService;

    @Autowired
    private TemplateService templateService;

    /**
     * 获取模板详情及规格列表
     */
    @GetMapping("/detail/{templateId}")
    public AjaxResult getProductSpecTemplateWithSpecs(@PathVariable("templateId") Long templateId) {
        ProductSpecTemplateDetail detail = productSpecTemplateService.getProductSpecTemplateWithSpecsById(templateId);
        return AjaxResult.success(detail);
    }

    /**
     * 查询规格属性模板列表
     */
    @PreAuthorize("@ss.hasPermi('product:productSpecTemplate:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductSpecTemplate productSpecTemplate)
    {
        startPage();
        List<ProductSpecTemplate> list = productSpecTemplateService.selectProductSpecTemplateList(productSpecTemplate);
        return getDataTable(list);
    }


    @PostMapping("/create")
    @Operation(summary = "创建模板")
    public AjaxResult createTemplate(@RequestBody TemplateCreateDto templateCreateDto) {
        try {
            Long templateId = templateService.createTemplate(templateCreateDto);
            return AjaxResult.success(templateId);
        } catch (Exception e) {
            return AjaxResult.error("创建模板失败: " + e.getMessage());
        }
    }

    @PutMapping("/update")
    @Operation(summary = "更新模板")
    public AjaxResult updateTemplate(@RequestBody TemplateCreateDto templateCreateDto) {
        try {
            if (templateCreateDto.getId() == null) {
                return AjaxResult.error("模板ID不能为空");
            }
            Long templateId = templateService.updateTemplate(templateCreateDto);
            return AjaxResult.success(templateId);
        } catch (Exception e) {
            return AjaxResult.error("更新模板失败: " + e.getMessage());
        }
    }

    /**
     * 删除规格属性模板
     */
    @PreAuthorize("@ss.hasPermi('product:productSpecTemplate:remove')")
    @Log(title = "规格属性模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productSpecTemplateService.deleteProductSpecTemplateByIds(ids));
    }

//    /**
//     * 导出规格属性模板列表
//     */
//    @PreAuthorize("@ss.hasPermi('product:productSpecTemplate:export')")
//    @Log(title = "规格属性模板", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, ProductSpecTemplate productSpecTemplate)
//    {
//        List<ProductSpecTemplate> list = productSpecTemplateService.selectProductSpecTemplateList(productSpecTemplate);
//        ExcelUtil<ProductSpecTemplate> util = new ExcelUtil<ProductSpecTemplate>(ProductSpecTemplate.class);
//        util.exportExcel(response, list, "规格属性模板数据");
//    }
//
//    /**
//     * 获取规格属性模板详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('product:productSpecTemplate:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return success(productSpecTemplateService.selectProductSpecTemplateById(id));
//    }
//
//    /**
//     * 新增规格属性模板
//     */
//    @PreAuthorize("@ss.hasPermi('product:productSpecTemplate:add')")
//    @Log(title = "规格属性模板", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody ProductSpecTemplate productSpecTemplate)
//    {
//        return toAjax(productSpecTemplateService.insertProductSpecTemplate(productSpecTemplate));
//    }
//
//    /**
//     * 修改规格属性模板
//     */
//    @PreAuthorize("@ss.hasPermi('product:productSpecTemplate:edit')")
//    @Log(title = "规格属性模板", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody ProductSpecTemplate productSpecTemplate)
//    {
//        return toAjax(productSpecTemplateService.updateProductSpecTemplate(productSpecTemplate));
//    }
}
